package com.shensi.appCollect.utils.yunHua;


import com.alibaba.fastjson.JSONObject;
import com.shensi.appCollect.utils.HttpUtils;
import com.shensi.appCollect.utils.MobilePhoneUtil;
import com.yunhua.oil.common.core.codec.encode.Encode;
import com.yunhua.oil.common.core.codec.factory.EncodeFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;

@Slf4j
@Component
public class yunHuaPayUtils {
    //向云化获取id
    private static String staticSyncYunUserHuaUrl;
    //向云化同步的地址
    private static String staticSyncToYunUserHuaUrl;

    @Value("${qujia.syncYunHuaUrl}")
    private String syncYunUserHuaUrl;

    @Value("${qujia.syncToYunHuaUrl}")
    private String syncToYunUserHuaUrl;


    @PostConstruct
    public void initSyncYunUserHuaUrl(){
        staticSyncYunUserHuaUrl = syncYunUserHuaUrl;
        staticSyncToYunUserHuaUrl = syncToYunUserHuaUrl;
    }




    public static HashMap yunHuaPay(YunHuaParam yunHuaParam){
        String  res="";
        try {
            log.info(yunHuaParam.toRequest());
//            res=  HttpUtils.connectPostUrl("http://123.60.22.157:31120/marketing/pay/unionPay", yunHuaParam.toRequest());
            res=  HttpUtils.connectPostUrl("https://upay.xailife.com/front/upi/preOrder", yunHuaParam.toRequest());
//            res=  HttpUtils.connectPostUrl("https://yh.xailife.com/marketing-dev01/pay/unionPay", yunHuaParam.toRequest());

            log.info("请求地址"+"https://upay.xailife.com/front/upi/preOrder");
             JSONObject result=JSONObject.parseObject(res);
            log.info("云华拉起支付返回值"+res);
            if(!"00000".equals(result.get("code")+"")){
                log.info("云华拉起支付失败，返回code"+result.get("code"));
                return new HashMap();
            }
            JSONObject data= JSONObject.parseObject(result.get("data")+"");
            if(!"T".equals(data.getString("status"))){
                log.info("云华支付，回调data的状态不正确，错误信息"+data.get("errMsg"));
                return new HashMap();
            }
            JSONObject  payParam=JSONObject.parseObject(data.getString("payInfo"));
            HashMap reqData = new HashMap();

                reqData.put("payTimeStamp", payParam.get("timeStamp"));
                reqData.put("payPackage", payParam.get("package"));
                reqData.put("paynonceStr", payParam.get("nonceStr"));
                reqData.put("paySignType", payParam.get("signType"));
                reqData.put("paySign", payParam.get("paySign"));
                reqData.put("merchantsNumber", data.get("txnOrderNo"));
            log.info(reqData.toString());
       return reqData;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new HashMap();

    }

    //todo 先写死小程序支付测试
    public static HashMap yunHuaPayWxMini(YunHuaParam yunHuaParam,String secretKey, String yunHuaPayUrl){
        String  res="";
        try {
            log.info(yunHuaParam.toRequest());
            JSONObject beforeSign=JSONObject.parseObject(yunHuaParam.toAfterSignRequestWxMini());
            String  sign=VerifySignUtil.getSign(beforeSign,secretKey);
            yunHuaParam.setSign(sign);
            log.info(yunHuaParam.toAfterSignRequestWxMini());
            //res=  HttpUtils.connectPostUrl("https://paytest01.xailife.com/front/upi/preOrder", yunHuaParam.toAfterSignRequest());
//            res=  HttpUtils.connectPostUrl("https://yh.xailife.com/marketing-dev01/pay/unionPay", yunHuaParam.toRequest());
            String param = "";
            res=  HttpUtils.connectPostUrl(yunHuaPayUrl, yunHuaParam.toAfterSignRequestWxMini());

            log.info("请求地址" + yunHuaPayUrl);
            JSONObject result=JSONObject.parseObject(res);
            log.info("云化拉起支付返回值"+res);
            if(!"0000".equals(result.get("code")+"")){
                log.info("云华拉起支付失败，返回code"+result.get("code"));
                return new HashMap();
            }

            HashMap reqData = new HashMap();
            reqData.put("redirectUrl",result.get("data")+"");
//            reqData.put("merchantsNumber", "112222233333");
            reqData.put("clientOrderNo", yunHuaParam.getPosOrderNo());
            reqData.put("clientSerialNo", yunHuaParam.getPosOrderNo());
            reqData.put("mno", yunHuaParam.getPartnerCode());

            log.info(reqData.toString());
            return reqData;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new HashMap();
    }

    public static HashMap yunHuaPayAli(YunHuaParam yunHuaParam,String secretKey, String yunHuaPayUrl){
        String  res="";
        try {
            log.info(yunHuaParam.toAfterSignRequestAliMini());
            JSONObject beforeSign=JSONObject.parseObject(yunHuaParam.toAfterSignRequestAliMini());
            String  sign=VerifySignUtil.getSign(beforeSign,secretKey);
            yunHuaParam.setSign(sign);
            log.info(yunHuaParam.toAfterSignRequestAliMini());
            //res=  HttpUtils.connectPostUrl("https://paytest01.xailife.com/front/upi/preOrder", yunHuaParam.toAfterSignRequest());
//            res=  HttpUtils.connectPostUrl("https://yh.xailife.com/marketing-dev01/pay/unionPay", yunHuaParam.toRequest());
            String param = "";
            res=  HttpUtils.connectPostUrl(yunHuaPayUrl, yunHuaParam.toAfterSignRequestAliMini());

            log.info("请求地址" + yunHuaPayUrl);
            JSONObject result=JSONObject.parseObject(res);

            log.info("云化拉起支付返回值"+res);
            if(!"0000".equals(result.get("code")+"")){
                log.info("云华拉起支付失败，返回code"+result.get("code"));
                return new HashMap();
            }

//            JSONObject data = JSONObject.parseObject(result.get("data").toString());
//            JSONObject data = JSON.parseObject(result.get("data"));

            HashMap reqData = new HashMap();
            reqData.put("redirectUrl",result.get("data"));
//            reqData.put("merchantsNumber", "112222233333");
            reqData.put("clientOrderNo", yunHuaParam.getPosOrderNo());
            reqData.put("clientSerialNo", yunHuaParam.getPosOrderNo());
            reqData.put("mno", yunHuaParam.getPartnerCode());
            log.info(reqData.toString());
            return reqData;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new HashMap();
    }

    public static HashMap yunHuaPayV2(YunHuaParam yunHuaParam,String secretKey, String yunHuaPrePayUrl){
        String  res="";
        try {
            log.info(yunHuaParam.toRequest());
            JSONObject beforeSign=JSONObject.parseObject(yunHuaParam.toAfterSignRequest());
            String  sign=VerifySignUtil.getSign(beforeSign,secretKey);
            yunHuaParam.setSign(sign);
            log.info(yunHuaParam.toAfterSignRequest());
            //res=  HttpUtils.connectPostUrl("https://paytest01.xailife.com/front/upi/preOrder", yunHuaParam.toAfterSignRequest());
//            res=  HttpUtils.connectPostUrl("https://yh.xailife.com/marketing-dev01/pay/unionPay", yunHuaParam.toRequest());
            res=  HttpUtils.connectPostUrl(yunHuaPrePayUrl, yunHuaParam.toAfterSignRequest());

            log.info("请求地址"+yunHuaPrePayUrl);
            JSONObject result=JSONObject.parseObject(res);
            log.info("云华拉起支付返回值"+res);
            if(!"0000".equals(result.get("code")+"")){
                log.info("云华拉起支付失败，返回code"+result.get("code"));
                return new HashMap();
            }

            HashMap reqData = new HashMap();
            reqData.put("redirectUrl",result.get("data")+"");
            reqData.put("clientOrderNo", yunHuaParam.getPosOrderNo());
            reqData.put("clientSerialNo", yunHuaParam.getPosOrderNo());
            reqData.put("mno", yunHuaParam.getPartnerCode());
            log.info("返回参数【{}】", reqData.toString());
            return reqData;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new HashMap();

    }

    public static String sendUser(String  mobliePhone){
        String  res="";
        if (MobilePhoneUtil.isFakePhone(mobliePhone)) {
            return res;
        }
        try {
             EncodeFactory encodeFactory=new EncodeFactory();
             YunHuaSendUser yunHuaSendUser=new YunHuaSendUser();
            yunHuaSendUser.setMobilePhone(mobliePhone);
            yunHuaSendUser.setPlatformId("106");
            yunHuaSendUser.setSystemId("10");
            Encode encode = encodeFactory.generateEncode("SHA256");
             String sign= encode.encode(JSONObject.parseObject(yunHuaSendUser.toBeforeRequest()),"4wv8umq3wd");
            yunHuaSendUser.setSign(sign);
//            res=  HttpUtils.connectPostUrl("http://123.60.22.157:31120/marketing/pay/unionPay", yunHuaParam.toRequest());
            String mmmmm=yunHuaSendUser.toRequest();
            log.info("向云化同步用户信息【{}】", staticSyncYunUserHuaUrl);
            res=  HttpUtils.connectPostUrl(staticSyncYunUserHuaUrl, mmmmm);

            JSONObject result=JSONObject.parseObject(res);
//            log.info("云华拉起支付返回值"+res);
//            if(!"0000".equals(result.get("code")+"")){
//                log.info("云华拉起支付失败，返回code"+result.get("code"));
//                return "";
//            }
//            JSONObject data= JSONObject.parseObject(result.get("data")+"");
//            if(!"T".equals(data.getString("status"))){
//                log.info("云华支付，回调data的状态不正确，错误信息"+data.get("errMsg"));
//                return "";
//            }
//            JSONObject  userParam=JSONObject.parseObject(data.getString("payInfo"));
            String  userId=result.getString("data");

            return userId;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }

    public static String syncUser(String  mobliePhone, String id){
        String  res="";
        if (MobilePhoneUtil.isFakePhone(mobliePhone)) {
            return res;
        }
        try {
            EncodeFactory encodeFactory=new EncodeFactory();
            YunHuaSyncUser yunHuaSyncUser=new YunHuaSyncUser();
            yunHuaSyncUser.setMobilePhone(mobliePhone);
            yunHuaSyncUser.setId(id);
            yunHuaSyncUser.setPlatformId("106");
            yunHuaSyncUser.setSystemId("10");
            Encode encode = encodeFactory.generateEncode("SHA256");
            String sign= encode.encode(JSONObject.parseObject(yunHuaSyncUser.toBeforeRequest()),"4wv8umq3wd");
            yunHuaSyncUser.setSign(sign);
//            res=  HttpUtils.connectPostUrl("http://123.60.22.157:31120/marketing/pay/unionPay", yunHuaParam.toRequest());
            String mmmmm=yunHuaSyncUser.toRequest();
            log.info("向云化同步用户信息【{}】", staticSyncToYunUserHuaUrl);
            res=  HttpUtils.connectPostUrl(staticSyncToYunUserHuaUrl, mmmmm);

            JSONObject result=JSONObject.parseObject(res);
//            log.info("云华拉起支付返回值"+res);
//            if(!"0000".equals(result.get("code")+"")){
//                log.info("云华拉起支付失败，返回code"+result.get("code"));
//                return "";
//            }
//            JSONObject data= JSONObject.parseObject(result.get("data")+"");
//            if(!"T".equals(data.getString("status"))){
//                log.info("云华支付，回调data的状态不正确，错误信息"+data.get("errMsg"));
//                return "";
//            }
//            JSONObject  userParam=JSONObject.parseObject(data.getString("payInfo"));
            String  userId=result.getString("data");

            return userId;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }



}
